<template>
  <vol-box
    :lazy="true"
    v-model="model"
    title="生产工位库存明细"
    :height="400"
    :width="1200"
    :padding="5"
    :onModelClose="onModelClose"
  >
    <produceUseKcmx ref="produceUseKcmx"></produceUseKcmx>
    <template #footer>
      <div>
        <el-button type="primary" size="small" @click="detailOnSelect"
          >确认</el-button
        >
      </div>
    </template>
  </vol-box>
</template>
    
    <script>
import VolBox from "@/components/basic/VolBox.vue";
import produceUseKcmx from "@/views/produceinfo/q_produce_use_kcmx/q_produce_use_kcmx.vue";
export default {
  components: {
    "vol-box": VolBox,
    produceUseKcmx,
  },
  data() {
    return {
      model: false,
      WHSE_Id: null,
    };
  },
  methods: {
    async onModelClose() {
      // console.log("关闭");
      // this.$refs.produceUseKcmx.searchFormFields.领用单号 = null;
      // this.$refs.produceUseKcmx.searchFormFields.领用人 = null;
      // this.$refs.produceUseKcmx.searchFormFields.产品编码 = null;
      // this.$refs.produceUseKcmx.searchFormFields.产品名称 = null;
      // this.$refs.produceUseKcmx.searchFormFields.sczt = null;
      // this.WHSE_Id = null;
      // await this.$refs.produceUseKcmx.$refs.grid.search();
    },
    async open(sub) {
      this.model = true;
      // this.WHSE_Id = WHSE_Id;
      let setIntervalTiem = setInterval(() => {
        if (this.$refs.produceUseKcmx) {
          clearInterval(setIntervalTiem);
          this.$refs.produceUseKcmx.searchFormFields.单号 = sub;
          this.$refs.produceUseKcmx.$refs.grid.search();
        }
      }, 100);
    },
    async detailOnSelect() {
      let rowObj = this.$refs.produceUseKcmx.$refs.grid.getSelectRows();

      if (rowObj.length) {
        this.$emit("parentCall", ($parent) => {
          let detailRowData = $parent.$refs.detail.rowData;

          let arr = [];
          let stringName = "";
          detailRowData.forEach((item) => {
            rowObj.forEach((j) => {
              if (j.配件编码 == item.Goods_sub) {
                stringName += stringName ? "," + j.配件名称 : j.配件名称;
                arr.push(j.配件名称);
              }
            });
          });
          if (stringName != "") {
            this.$message.warning("明细表已存在 " + stringName);
          }
          arr.forEach((item) => {
            let index = rowObj.findIndex((x) => x.配件名称 == item);
            if (index != -1) {
              rowObj.splice(index, 1);
            }
          });
          let newRowObj = [];
          newRowObj = rowObj.map((item) => {
            return {
              // Produce_use: item.ID,
              goodsInventoryWarehouseXm: item.转入库存id,
              Goods_sub: item.配件编码,
              Goods_name: item.配件名称,
              Goods_num: 1,
              // Goods_size: item.配件规格,
              WHSE_name: item.转入库位,
              Type: "0",
              AvailableQuantity: item.可用数量,
              Cost_price: item.转入成本,
              WHSE_no:item.生产工位,
              Tax_exclusive: item.转入无税成本,
              Cost_accounting: item.成本核算方式,
              WHSE_scgw_id:item.工位库存id,
            };
          });
          // newRowObj = this.base.getPushDatileWHSEName(
          //   newRowObj,
          //   "Goods_sub",
          //   this.WHSE_Id
          // );
          $parent.$refs.detail.rowData.unshift(...newRowObj);
          // $parent.WHSE_IdDisableFn();
          // this.getDatileSelectFn($parent, "Goods_sub");
        });

        this.onModelClose();
        return (this.model = false);
      } else {
        if (this.messageError) {
          this.messageError.close();
        }
        this.messageError = this.$message.error("请至少选择一条单据");
      }
    },
    //明细表库位下拉框
    async getDatileSelectFn($parent, sub) {
      let rows = $parent.$refs.detail.rowData;
      await rows.forEach((row) => {
        $parent.$refs.detail.columns.forEach((item) => {
          if (item.field == "WHSE_name") {
            let selectUrl = `api/q_warehouse_locationList/getgoodslocation?bm=${row[sub]}`;
            item.bind.data.forEach((d) => {
              d.hidden = true;
            });
            this.http.post(selectUrl, {}, "").then((reslut) => {
              item.bind.data.forEach((d) => {
                d.hidden = true;
              });
              item.bind.data.forEach((d) => {
                reslut.rows.forEach((j) => {
                  if (d.key == j.WHSE_no) {
                    d.hidden = false;
                  }
                });
              });
              row.WHSE_name = item.bind.data.filter(
                (x) => x.hidden == false
              )[0].key;
            });
          }
        });
      });
    },
  },
};
</script>
    
    <style>
</style>